//
//  Standing.m
//  ChampoinPeso
//
//  Created by mac on 2025/2/18.
//

#import "Standing.h"
#import "Text.h"
#import "Select.h"
#import "InfoModel.h"
#import "SelectPop.h"
#import "LocationUpdata.h"
#import "SelectAddress.h"
@interface Standing ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>
@property (nonatomic,strong)UIButton *apply;
@property (nonatomic,strong)NSMutableArray *items;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic,strong)SelectAddress *addressview;

@end

@implementation Standing

-(SelectAddress *)addressview
{
    if (!_addressview) {
        _addressview =  [[SelectAddress alloc]initWithFrame:CGRectZero];
    }
    return _addressview;
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self standingSource];
}

-(NSMutableArray *)items
{
    if (!_items) {
        _items = [NSMutableArray array];
    }
    return _items;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navtitleText = self.name;
    UIImageView *bgIV = [[UIImageView alloc] initWithFrame:self.view.bounds];
    bgIV.image = [UIImage imageNamed:@"standing_bg"];
    [self.view addSubview:bgIV];
    [[LocationUpdata sharedInstance] startMarkTag];
    [self navUI];
    [self setupUI];
}

-(void)standingSource
{
//    /cham/fair
    NSDictionary *params = @{@"gossip":_pid};
    [DataFrameMove frameMoverForPost:@"/cham/fair" MoverPost:YES frameParm:params finish:^(id  _Nonnull info) {
        NSArray *items = info[@"fashion"][@"kick"];
        [items enumerateObjectsUsingBlock:^(NSDictionary*  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            InfoModel *model = [InfoModel new];
            model.supposing = CPS(obj[@"supposing"]);
            model.soldierlylooking = CPS(obj[@"soldierlylooking"]);
            model.gardening = CPS(obj[@"gardening"]);
            model.youre = CPS(obj[@"youre"]);
            model.mosquito = CPS(obj[@"mosquito"]);
            model.delivered = obj[@"delivered"];
            model.inquired = CPS(obj[@"inquired"]);
            NSArray *items = obj[@"delivered"];
            [items enumerateObjectsUsingBlock:^(NSDictionary*  _Nonnull obj1, NSUInteger idx, BOOL * _Nonnull stop) {
                if ([CPS(obj[@"inquired"]) isEqualToString:obj1[@"madnevertheless"]]) {
                    model.herin = CPS(obj1[@"herin"]) ;
                }
            }];
            [self.items addObject:model];
        }];
        [self.tableView reloadData];
    }];
}

-(void)setupUI
{
    [self.view addSubview:self.apply];
    [_apply mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_offset(-bottomSafeHeight-5);
        make.left.mas_offset(24);
        make.right.mas_offset(-24);
        make.height.mas_equalTo(52);
    }];
    UIView *view = [UIView new];
    view.backgroundColor = UIColor.whiteColor;
    view.layer.cornerRadius = 16;
    [self.view addSubview:view];
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(_apply.mas_top).offset(-20);
        make.left.mas_offset(13);
        make.right.mas_offset(-13);
        make.top.mas_offset(98+64+MarginTop+20);
    }];
    [view addSubview:self.tableView];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.mas_offset(0);
    }];
    [_apply addTarget:self action:@selector(applyNext) forControlEvents:UIControlEventTouchUpInside];
}

-(void)applyNext
{
    //   /cham/role
    NSMutableArray *items = [NSMutableArray array];
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [self.items enumerateObjectsUsingBlock:^(InfoModel *  _Nonnull model, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([CPS(model.herin)  isEqualToString:@""]) {
            [dict setObject:model.inquired forKey:model.gardening];
        }else{
            [dict setObject:model.herin forKey:model.gardening];
        }
        [items addObject:dict];
    }];
    [dict setObject:_pid forKey:@"gossip"];
    [DataFrameMove frameMoverForPost:@"/cham/role" MoverPost:YES frameParm:dict finish:^(id  _Nonnull info) {
        if ([info[@"gardening"] integerValue] == 0) {
            [self startMark:@"5"];
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            [self showAutoMessage:info[@"hobby"] ToView:self.view];
        }
    }];
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    __block InfoModel *model = self.items[textField.tag];
    if ([model.youre isEqualToString:@"Forbesa"]) {
        [[SelectPop getInstance] oneSelect:model key:1];
        [SelectPop getInstance].commitAction = ^(BRResultModel * _Nonnull model1) {
            model.herin = model1.key;
            model.inquired = model1.value;
            [self.items replaceObjectAtIndex:textField.tag withObject:model];
            [self->_tableView reloadData];
        };
        [self.view endEditing:YES];
        return NO;
    }else  if ([model.youre isEqualToString:@"Forbesb"]) {
        return YES;
    }else  if ([model.youre isEqualToString:@"Forbesc"]) {
        [self cityTest:textField.tag];
        [self.view endEditing:YES];
        return NO;
    }
    return YES;
}
-(void)cityTest:(NSInteger)index{
    [DataFrameMove frameMoverForPost:@"/cham/room" MoverPost:NO frameParm:@{} finish:^(id  _Nonnull info) {
        InfoModel *model = self.items[index];
        [self.view addSubview:self.addressview];
        [self.addressview mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, 354));
            make.bottom.mas_offset(-bottomSafeHeight-10);
        }];
        [self.addressview setSource:info[@"fashion"] name:model.inquired];
        self.addressview.closeAction = ^{
            [self.addressview removeFromSuperview];
        };
        self.addressview.dddressCommitAction = ^(NSArray<BRResultModel *> * _Nonnull resultModelArr) {
            [self.addressview removeFromSuperview];
            if (resultModelArr.count == 2) {
                model.inquired = [NSString stringWithFormat:@"%@|%@",resultModelArr[0].value,resultModelArr[1].value];
            }else{
                model.inquired = [NSString stringWithFormat:@"%@|%@|%@",resultModelArr[0].value,resultModelArr[1].value,resultModelArr[2].value];
            }
            [self.items replaceObjectAtIndex:index withObject:model];
            [self->_tableView reloadData];
            NSLog(@"%@",resultModelArr);
        };
    }];
}

-(BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    __block InfoModel *model = self.items[textField.tag];
    if ([model.youre isEqualToString:@"Forbesb"]) {
        model.inquired = textField.text;
        [_tableView reloadData];
        return YES;
    }
    return NO;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.items.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    Select *cell =  [[Select alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass([Select class])];
    cell.backgroundColor = UIColor.clearColor;
    InfoModel *model = self.items[indexPath.row];
    if (![model.inquired isEqualToString:@""]) {
        cell.name.text = model.inquired;
    }
    cell.titleLabel.text = model.supposing;
    cell.name.tag = indexPath.row;
    cell.name.delegate = self;
    cell.placeHolder = model.soldierlylooking;
    if ([model.youre isEqualToString:@"Forbesa"]) {
        cell.canEdit = NO;
        cell.detailImg.image = [UIImage imageNamed:@"down"];
    }else  if ([model.youre isEqualToString:@"Forbesb"]) {
        cell.canEdit = YES;
        cell.detailImg.image = [UIImage imageNamed:@""];
    }else  if ([model.youre isEqualToString:@"Forbesc"]) {
        cell.canEdit = NO;
        cell.detailImg.image = [UIImage imageNamed:@"down"];
    }
    if ([model.mosquito isEqualToString:@"1"]) {
        cell.name.keyboardType = UIKeyboardTypeNumberPad;
    }else{
        cell.name.keyboardType = UIKeyboardTypeDefault;
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

-(UIButton *)apply
{
    if (!_apply) {
        _apply = [UIButton buttonWithType:UIButtonTypeCustom];
        [_apply setImage:[UIImage imageNamed:@"auth_next"] forState:UIControlStateNormal];
    }
    return _apply;
}

-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.backgroundColor = UIColor.clearColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.bounces = NO;
    }
    return _tableView;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
